﻿using IOP.SgrA;
using IOP.SgrA.ECS;
using IOP.SgrA.Input;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Text;
using IOP.Extension.DependencyInjection;

namespace VkSample96
{
    public class InputSystem : ComponentSystem
    {
        private Camera Camera;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="contextManager"></param>
        public InputSystem()
        {
        }

        public override void Initialization()
        {
            ContextManager.Foreach<TransformGroup>((group) =>
            {
                Context context = ContextManager.GetContext(group.Entity);
                var render = context.GetContextRenderGroup();
                var camera = render.Camera;
                Camera = camera;
            });
        }

        public override void Update(TimeSpan lastStamp)
        {
            if (Input.GetMouseButtonState(MouseButton.ButtonLeft).Action == InputAction.Press)
            {
                var delta = Input.GetMousePositionDelta();
                if (delta.X != 0 || delta.Y != 0)
                {
                    Camera.EncircleFromMouse(Vector3.Zero, delta.X, delta.Y, (float)(0.1 * Math.PI / 180.0));
                }
            }
            int w = Input.GetMouseWheelDelta();
            if (w != 0)
            {
                Camera.Scroll(w * 0.02f);
            }
            ContextManager.Foreach<TransformGroup>((group) =>
            {
                Context context = ContextManager.GetContext(group.Entity);
                ref MVPMatrix mvp = ref context.GetMVPMatrix();
                context.PushToRenderGroup();
            });
        }
    }

    public struct TransformGroup
    {
        public Entity Entity;
        public TransformComponent Transform;
    }
}
